package org.wzq.android.mdm_api;

import android.app.Activity;
import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import com.prashant.adesara.device.admin.R;


/**
 * MainActivity to enable the Device Admin Policy.
 * @author Prashant Adesara
 */
public class MainActivity extends Activity {
    private static final int REQUEST_CODE = 0;
    private DevicePolicyManager mDPM;
    private ComponentName mAdminName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        try {
            System.out.println("Initiate DevicePolicyManager");
            // Initiate DevicePolicyManager.
            mDPM = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
            // Set DeviceAdminDemo Receiver for active the component with different option
            mAdminName = new ComponentName(this, DeviceAdminDemo.class);
            if(!mDPM.isAdminActive(mAdminName)) {
                // try to become active
                Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
                intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, mAdminName);
                //intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION,"Click on Activate button to secure your application.");
                startActivityForResult(intent, REQUEST_CODE);
            } else {
                // Already is a device administrator, can do security operations now.
                // mDPM.lockNow();
            }
        } catch(Exception e) {
            e.printStackTrace();
        }

        init();
    }

    private void init() {
        // 清除数据
        findViewById(R.id.btn_wipe_data).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
            }
        });

        // 更改锁屏密码 OK
        findViewById(R.id.btn_change_lock_password).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                mDPM.resetPassword("123456", 0);
            }
        });

        // 密码规则
        findViewById(R.id.btn_password_policy).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                mDPM.setPasswordMinimumLength(mAdminName, 6);
            }
        });

        // 屏幕解锁尝试次数
        findViewById(R.id.btn_max_failed_password_attempts).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                //mDPM.setMaximumFailedPasswordsForWipe(mAdminName,10);
            }
        });

        // lock screen OK
        findViewById(R.id.btn_lock).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                mDPM.lockNow();// 锁屏
            }
        });

        // 密码有效期
        findViewById(R.id.btn_password_expire).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                mDPM.setPasswordExpirationTimeout(mAdminName, 0);
            }
        });

        // 设置存储设备密码
        findViewById(R.id.btn_crypt_storage).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // mDPM.setStorageEncryption(mAdminName, true);
            }
        });
        // 相机停用 OK
        findViewById(R.id.btn_disable_camera).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                mDPM.setCameraDisabled(mAdminName, true);
            }
        });
    }

    /**
     * @author Prashant Adesara
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        System.out.println("onActivityResult");
        if(REQUEST_CODE == requestCode) {
            if(resultCode == Activity.RESULT_OK) {
                System.out.println("RESULT_OK");
                // done with activate to Device Admin
            } else {
                System.out.println("RESULT_fail");
                // cancle it.
            }
        }
    }
}